package com.anbang.app.ui.animation.views

import android.animation.ArgbEvaluator
import android.content.Context
import android.widget.FrameLayout
import com.anbang.framework.utils.log

/**
 * @Description:
 * @Author: AnBang
 * @Date: 2019/1/18 10:38
 */
class DiscrollvableView(context: Context) : FrameLayout(context), DiscrollvableInterface {

    /**
     * <attr name="discrollve_translation">
     * <flag name="fromTop" value="0x01"></flag>
     * <flag name="fromBottom" value="0x02"></flag>
     * <flag name="fromLeft" value="0x04"></flag>
     * <flag name="fromRight" value="0x08"></flag>
    </attr> *
     * 0000000001
     * 0000000010
     * 0000000100
     * 0000001000
     *
     *
     * 0000000101
     */
    private val TRANSLATION_FROM_TOP = 0x01
    private val TRANSLATION_FROM_BOTTOM = 0x02
    private val TRANSLATION_FROM_LEFT = 0x04
    private val TRANSLATION_FROM_RIGHT = 0x08

    //颜色估值器
    private val sArgbEvaluator = ArgbEvaluator()
    /**
     * 自定义属性的一些接收的变量
     */
    private var mDiscrollveFromBgColor: Int = 0//背景颜色变化开始值
    private var mDiscrollveToBgColor: Int = 0//背景颜色变化结束值
    private var mDiscrollveAlpha: Boolean = false//是否需要透明度动画
    private var mDisCrollveTranslation: Int = 0//平移值
    private var mDiscrollveScaleX: Boolean = false//是否需要x轴方向缩放
    private var mDiscrollveScaleY: Boolean = false//是否需要y轴方向缩放
    private var mHeight: Int = 0//本view的高度
    private var mWidth: Int = 0//宽度

    fun setmDiscrollveFromBgColor(mDiscrollveFromBgColor: Int) {
        this.mDiscrollveFromBgColor = mDiscrollveFromBgColor
    }

    fun setmDiscrollveToBgColor(mDiscrollveToBgColor: Int) {
        this.mDiscrollveToBgColor = mDiscrollveToBgColor
    }

    fun setmDiscrollveAlpha(mDiscrollveAlpha: Boolean) {
        this.mDiscrollveAlpha = mDiscrollveAlpha
    }

    fun setmDisCrollveTranslation(mDisCrollveTranslation: Int) {
        this.mDisCrollveTranslation = mDisCrollveTranslation
    }

    fun setmDiscrollveScaleX(mDiscrollveScaleX: Boolean) {
        this.mDiscrollveScaleX = mDiscrollveScaleX
    }

    fun setmDiscrollveScaleY(mDiscrollveScaleY: Boolean) {
        this.mDiscrollveScaleY = mDiscrollveScaleY
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = w
        mHeight = h
        onResetDiscrollve()
    }


    override fun onDiscrollve(ratio: Float) {

        log("========================ratio=$ratio")

        // ratio:0~1
        //控制自身的动画属性
        if (mDiscrollveAlpha) {
            alpha = ratio
        }
        if (mDiscrollveScaleX) {
            scaleX = ratio
        }
        if (mDiscrollveScaleY) {
            scaleY = ratio
        }

        //		int mDisCrollveTranslation 有很多种枚举的情况

        //判断到底是哪一种值：fromTop,fromBottom,fromLeft,fromRight
        //fromBottom
        if (isDiscrollTranslationFrom(TRANSLATION_FROM_BOTTOM)) {
            translationY = mHeight * (1 - ratio)//mHeight-->0(代表原来的位置)
        }
        if (isDiscrollTranslationFrom(TRANSLATION_FROM_TOP)) {
            translationY = -mHeight * (1 - ratio)//-mHeight-->0(代表原来的位置)
        }
        if (isDiscrollTranslationFrom(TRANSLATION_FROM_LEFT)) {
            translationX = -mWidth * (1 - ratio)//-width-->0(代表原来的位置)
        }
        if (isDiscrollTranslationFrom(TRANSLATION_FROM_RIGHT)) {
            translationX = mWidth * (1 - ratio)//width-->0(代表原来的位置)
        }

        //颜色渐变动画
        if (mDiscrollveFromBgColor != -1 && mDiscrollveToBgColor != -1) {
            //ratio=0.5 color=中间颜色
            setBackgroundColor(sArgbEvaluator.evaluate(ratio, mDiscrollveFromBgColor, mDiscrollveToBgColor) as Int)
        }

    }

    private fun isDiscrollTranslationFrom(translationMask: Int): Boolean {
        return if (mDisCrollveTranslation == -1) {
            false
        } else mDisCrollveTranslation and translationMask == translationMask
        //fromLeft|fromBottom & fromBottom = fromBottom
    }


    override fun onResetDiscrollve() {
        //控制自身的动画属性
        if (mDiscrollveAlpha) {
            alpha = 0f
        }
        if (mDiscrollveScaleX) {
            scaleX = 0f
        }
        if (mDiscrollveScaleY) {
            scaleY = 0f
        }

        //		int mDisCrollveTranslation 有很多种枚举的情况

        //判断到底是哪一种值：fromTop,fromBottom,fromLeft,fromRight
        //fromBottom
        if (isDiscrollTranslationFrom(TRANSLATION_FROM_BOTTOM)) {
            translationY = mHeight.toFloat()//mHeight-->0(代表原来的位置)
        }
        if (isDiscrollTranslationFrom(TRANSLATION_FROM_TOP)) {
            translationY = (-mHeight).toFloat()//-mHeight-->0(代表原来的位置)
        }
        if (isDiscrollTranslationFrom(TRANSLATION_FROM_LEFT)) {
            translationX = (-mWidth).toFloat()//-width-->0(代表原来的位置)
        }
        if (isDiscrollTranslationFrom(TRANSLATION_FROM_RIGHT)) {
            translationX = mWidth.toFloat()//width-->0(代表原来的位置)
        }

    }
}